package com.sc.sso.controller;

import com.sc.common.pojo.CommonResult;
import com.sc.pojo.TbUser;
import com.sc.sso.service.SsoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @auther sc
 * @Date 2017/11/5
 */
@Controller
@RequestMapping("/user")
public class SsoController {

    @Autowired
    private SsoService ssoService;

    @RequestMapping("/check/{param}/{type}")
    @ResponseBody
    public Object paramCheck(@PathVariable("param") String param,
                             @PathVariable("type") Integer type,
                             String callback) {
        CommonResult result = ssoService.checkData(param, type);
        if (StringUtils.isNoneBlank(callback)) {
            //jsonp
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
            mappingJacksonValue.setJsonpFunction(callback);
            return mappingJacksonValue;
        }
        return result;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult register(TbUser user) {
        return ssoService.register(user);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestParam("username") String username,
                              @RequestParam("password") String password,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        return ssoService.login(username, password, request, response);

    }


    @RequestMapping("/token/{token}")
    @ResponseBody
    public Object getUserByToken(@PathVariable("token") String token, String callback) {
        CommonResult result = ssoService.getUserByToken(token);
        if (StringUtils.isNoneBlank(callback)) {
            //jsonp
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
            mappingJacksonValue.setJsonpFunction(callback);
            return mappingJacksonValue;
        }
        return result;
    }

    @RequestMapping("/logout/{token}")
    @ResponseBody
    public Object logout(@PathVariable("token")String token,String callback,
                         HttpServletRequest request,
                         HttpServletResponse response){
        CommonResult commonResult = ssoService.logout(token,request,response);
        if (StringUtils.isNoneBlank(callback)) {
            //jsonp
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(commonResult);
            mappingJacksonValue.setJsonpFunction(callback);
            return mappingJacksonValue;
        }
        return "redirect:login";
    }


}